package com.xopencode.mvc;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author Mcb
 * @date 2022/3/27
 * @apiNote
 */
@SpringBootApplication
public class MyFilter implements Filter {
    /**
     * 服务器启动即执行。资源初始化
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    /**
     * 拦截请求的方法，在此方法中可以对资源实现管理。需要手动对请求进行放行。chain.doFilter(request, response);
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        for (Enumeration<String> headerNames = req.getHeaderNames();headerNames.hasMoreElements();){
            String s = headerNames.nextElement();
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }

    /**
     * 服务器关闭执行。
     */
    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
